package com.egg.logger.controller.web;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.model.response.web.UserPage;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.common.util.OptionalUtil;
import com.egg.logger.entity.access.Access;
import com.egg.logger.model.request.AccessPost;
import com.egg.logger.model.response.AccessInfo;
import com.egg.logger.model.response.AccessPage;
import com.egg.logger.service.FeignService;
import com.egg.logger.service.access.IAccessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 访问 前端控制器
 * </p>
 *
 * @author east
 * @since 2021-09-13
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "访问日志")
@RestController
@RequestMapping("/access")
public class WebAccessController extends WebController {


    @Autowired
    private IAccessService accessService;
    @Autowired
    private FeignService feignService;


    @ApiOperation(value = "日志访问新增")
    @PostMapping
    public ResponseData<String> accessPost(@RequestBody AccessPost param) {
        if (null == param) {
            return ResponseData.success();
        }
        Long id = accessService.add(param);
        return ResponseData.success(id.toString());
    }

    @ApiOperation(value = "日志访问分页")
    @GetMapping("/page")
    public ResponseData<PageData<AccessPage>> accessPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @RequestParam(value = "userId", required = false) @ApiParam(value = "用户ID") Long userId
            , @RequestParam(value = "createDateFrom", required = false) @ApiParam(value = "开始时间") String createDateFrom
            , @RequestParam(value = "createDateTo", required = false) @ApiParam(value = "结束时间") Integer createDateTo
    ) {
        Page<Access> dbPage = accessService.page(
                current, pageSize
                , OptionalUtil.build(userId)
                , OptionalUtil.build(createDateFrom)
                , OptionalUtil.build(createDateTo)
        );
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage, Collections.emptyList()));
        }
        List<UserPage> userList = feignService.userList(dbPage.getRecords().stream().map(o -> o.getUserId()).distinct().collect(Collectors.toList()));
        Map<Long, UserPage> userMap = userList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o));
        List<AccessPage> list = new ArrayList<>();
        dbPage.getRecords().forEach(o -> {
            AccessPage temp = JsonUtil.objToObj(o, AccessPage.class);
            list.add(temp);
            UserPage user = userMap.getOrDefault(o.getUserId(), new UserPage());
            temp.setRealName(user.getRealName());
        });
        return ResponseData.success(new PageData<>(dbPage, list));
    }

    @ApiOperation(value = "日志访问信息")
    @GetMapping("/{id}/info")
    public ResponseData<AccessInfo> accessInfoGet(@ApiParam(value = "日志ID", required = true) @PathVariable String id) {
        Access access = accessService.baseGetById(id);
        Optional<UserPage> user = feignService.userInfo(access.getUserId());
        AccessInfo accessInfo = JsonUtil.objToObj(access, AccessInfo.class);
        user.ifPresent(o -> accessInfo.setRealName(o.getRealName()));
        return ResponseData.success(accessInfo);
    }


}

